/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <aos/aos.h>
#include <aos/kernel.h>

#include <aos/hal/spi.h>
#include <drv/gpio.h>
#include <ulog/ulog.h>
#include <yoc/atserver.h>
#include <pin_name.h>
#include <pinmux.h>
#include "pin_config.h"

#ifdef TEST_SPI
// #define SPI_PORT_NUM  1
#define SPI_BUF_SIZE   5
#define SPI_BUF_SIZE2   1
#define SPI_TX_TIMEOUT 10000
#define SPI_RX_TIMEOUT 10000

#define TAG "TEST_HAL_SPI"

/* define dev */
spi_dev_t spi1;
gpio_pin_handle_t gpio;

static void aos_hal_spi_master(int size,int frequency,int tx_time,int rx_time)
{
    int ret1     = -1;
    int ret2     = -1;
    int ret3     = -1;
    int ret2_result     = 0;
    int i       = 0;
    int n        = 0;
    uint8_t spi_data_buf[size+1];
    uint8_t spi_data_buf1[SPI_BUF_SIZE+1]={0};
    spi1.config.freq = frequency;

    memset(spi_data_buf, 0x0, size+1);
    memset(spi_data_buf1, 0x0, SPI_BUF_SIZE+1);

    ret1 = hal_spi_init(&spi1);
    
    if (ret1 != 0) {
        printf("spi1 init error !\n");
    }
    else{
        printf("spi1 init success !\n");
    }

    for (i = 0; i < size; i++) {
        spi_data_buf[i] = i + 2;
    }


    while(n<10) {
        ret2 = hal_spi_send_recv(&spi1,spi_data_buf,spi_data_buf1,size, rx_time);
        aos_msleep(400);
        for (int i = 0; i < size; i++)
        {
            printf("master send is %d\n",spi_data_buf[i]);
            printf("master recv is %d\n",spi_data_buf1[i]);
            if(spi_data_buf1[i] != spi_data_buf[i]){
                ret2_result += 1;
            }
        }
        
        if (ret2 == 0) {
            printf("spi1 data recv succeed !\n");
        }
        else
        {
            printf("spi1 data recv error !\n");
        }
        n++;
    };

    ret3 = hal_spi_finalize(&spi1);
    if (ret3 != 0) {
        printf("spi1 finalize error !\n");
    }

    if (ret1 == 0 && ret2 == 0 && ret2_result == 0 && ret3 == 0) {
        AT_BACK_OK();
    }
    else{
        AT_BACK_ERR();
    }
}


void test_hal_spi(char *cmd, int type, char *data)
{    
    drv_pinmux_config(SPI_CS,SPI_CS_FUNC_NO_SLAVE);
    drv_pinmux_config(SPI_SCK,SPI_SCK_FUNC_NO_SLAVE);
    drv_pinmux_config(SPI_MISO,SPI_MISO_FUNC);
    drv_pinmux_config(SPI_MOSI,SPI_MOSI_FUNC);

    spi1.port = SPI_PORT_NUM;
    spi1.config.mode  = HAL_SPI_MODE_MASTER;

    if (strcmp((const char *)data, "'send_multi'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE,100000,SPI_TX_TIMEOUT,SPI_RX_TIMEOUT);
    }else if (strcmp((const char *)data, "'send_single'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE2,100000,SPI_TX_TIMEOUT,SPI_RX_TIMEOUT);
    }else if (strcmp((const char *)data, "'freq_1000'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE,1000,SPI_TX_TIMEOUT,SPI_RX_TIMEOUT);
    }else if (strcmp((const char *)data, "'freq_1M'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE,1000000,SPI_TX_TIMEOUT,SPI_RX_TIMEOUT);
    }else if (strcmp((const char *)data, "'freq_10M'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE,10000000,SPI_TX_TIMEOUT,SPI_RX_TIMEOUT);
    }else if (strcmp((const char *)data, "'freq_3M'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE,3000000,SPI_TX_TIMEOUT,SPI_RX_TIMEOUT);
    }else if (strcmp((const char *)data, "'timeout_HAL_WAIT_FOREVER'\0") == 0) {
        aos_hal_spi_master(SPI_BUF_SIZE,100000,SPI_TX_TIMEOUT,HAL_WAIT_FOREVER);
    }
}

#endif